// 功能：设置cookie
// 参数：键，值，有效时长（天数）
function setCookie(key, value, date) {
    let d = new Date();
    d.setDate(d.getDate() + date);

    document.cookie = `${key}=${value};expires=${d};path=/`;
}

// 功能：删除cookie
// 参数：删除的键名
function removeCookie(key) {
    setCookie(key, 'beybey', -1);
}

// 功能：获取指定键的cookie
// 参数：键名
function getCookie(key) {
    let cookieStr = document.cookie;
    let initArr = cookieStr.split('; ');
    for (let i in initArr) {
        if (initArr[i].startsWith(key + '=')) {
            return initArr[i].split('=')[1];
        }
    }
}

// 判断是否是第一次登陆
function isFirstLogin() {
    let cookie = getCookie('username');
    if (cookie) {
        $('.topyou1').css("display","block").html(`
                    <a class="topyoutu" href="javascript:;">
                        <img src="../imges/indeximg/yx/Lillia.png" alt="javascript:;">
                    </a>
                    <b class="mingzi"><a href="">${cookie}</a></b>
                    <p class="topyouzi"><a href="./index.html" target="_blank">退出</a></p>
        `);
        $(".topyou").css("display","none")
    }
}
isFirstLogin()
// 点击退出登录按钮退出登录并跳转
function logout() {
    $('.topyou1').on('click', '.topyouzi', function () {
        removeCookie('username');
        $(".topyou").css("display","block")
        location.reload(true);
    });
}
logout()

$(function(){
    getGoods()
})
////////////////////////////////////////商城也的商品接口///////////////////////////////////////////
function  getGoods(){
    $.get("../goodsAndShoppingCart/getGoodsList.php",{"typeId":"004"},function(data){
        let str = '';
        data.forEach(function(item,index){
            str+=`
            <li class="fa"  goodsid="${item.goodsId}">
                <a >
                    <img src="${item.goodsImg}" alt="">
                    <p>${item.goodsName}</p>
                    <div class="jiage">
                        <span>${item.goodsDesc}</span>
                        <span>${item.goodsPrice}元</span>
                    </div>
                </a>
                <b>
                    <a>立即购买</a>
                </b>
            </li>
            `
        })
        $("#ul2").html(str)
    },"json")
}

$('#ul2').on('click',".fa",function(){
    let id=$(this).attr('goodsId')
    console.log(id);
    location.href='../html/detailsPage.html?goodsId='+id
})

